<?php
declare(strict_types=1);
namespace SixShop\Lakala\Service;

use SixShop\Lakala\Config;
use SixShop\Lakala\OpenAPISDK\V2\Api\V2LakalaApi;
use SixShop\Lakala\OpenAPISDK\V3\Api\LakalaApi;
use SixShop\Lakala\OpenAPISDK\V3\Model\ModelRequest;

class SACSService
{
    private LakalaApi $lakalaApi;

    public function __construct(private Config $config)
    {
        $this->lakalaApi = new LakalaApi($config->getV3Config());
    }

    /**
     * 余额分账
     *
     * @link https://o.lakala.com/#/home/document/detail?id=489
     */
    public function balanceSeparate(array $reqData)
    {
        $request = new ModelRequest();
        $request->setReqData($reqData);
        $response = $this->lakalaApi->tradeApi('/api/v3/sacs/balanceSeparate', $request);
        if ($response->getRespData()) {
            print_r($response->getRespData());
        } else {
            print_r($response);
        }
        echo $response->getRetCode();

        # 响应头信息
        print_r($response->getHeaders());

        # 响应原文
        echo $response->getOriginalText();
    }

    /**
     * 分账结果查询
     *
     * @param string $merchantNo 商户编号
     * @param string $separateNo 分账单号
     * @param string $outSeparateNo 商户系统分账单号
     * @link https://o.lakala.com/#/home/document/detail?id=492
     */
    public function balanceSeparateQuery(string $merchantNo, string $separateNo = '', string $outSeparateNo = '')
    {
        $request = new ModelRequest();
        $reqData = [
            'merchant_no' => $merchantNo,
        ];
        if ($separateNo) {
            $reqData['separate_no'] = $separateNo;
        }
        if ($outSeparateNo) {
            $reqData['out_separate_no'] = $outSeparateNo;
        }
        $request->setReqData($reqData);
        $response = $this->lakalaApi->tradeApi('/api/v3/sacs/balanceSeparateQuery', $request);
        if ($response->getRespData()) {
            print_r($response->getRespData());
        } else {
            print_r($response);
        }
        echo $response->getRetCode();

        # 响应头信息
        print_r($response->getHeaders());

        # 响应原文
        echo $response->getOriginalText();
    }
}